/**
 * 
 */
package com.android.dwt.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ProgressBar;

/**
 * @author xiang
 *
 */
public class TextProgressBar extends ProgressBar {
	
	private final int PADDING_LEFT = 10;
	private final int PADDING_TOP = 0;
	private final int PADDING_RIGHT = 10;
	private final int PADDING_BOTTOM = 20;
	
    private String mText;  
    private Paint mPaint;  
  
    public TextProgressBar(Context context) {
        super(context);
        
        mText = "";  
        mPaint = new Paint();
        mPaint.setColor(Color.BLACK);
        
        this.setPadding(PADDING_LEFT, PADDING_TOP, PADDING_RIGHT, PADDING_BOTTOM);
    }
  
    public TextProgressBar(Context context, AttributeSet attrs) {  
        super(context, attrs);
        
        mText = "";  
        mPaint = new Paint();  
        mPaint.setColor(Color.BLACK);
        
        this.setPadding(PADDING_LEFT, PADDING_TOP, PADDING_RIGHT, PADDING_BOTTOM);
    }  
  
    public TextProgressBar(Context context, AttributeSet attrs, int defStyle) {  
        super(context, attrs, defStyle);
        
        mText = "";  
        mPaint = new Paint();  
        mPaint.setColor(Color.BLACK);
        
        this.setPadding(PADDING_LEFT, PADDING_TOP, PADDING_RIGHT, PADDING_BOTTOM);
    }  
  
    @Override  
    protected synchronized void onDraw(Canvas canvas) {
        // First draw the regular progress bar, then custom draw our text  
        super.onDraw(canvas);
        
        Rect bounds = new Rect();  
        mPaint.getTextBounds(mText, 0, mText.length(), bounds);
        
        int x = getWidth()/2 - bounds.centerX();  
        int y = getHeight()-2;
        
        canvas.drawText(mText, x, y, mPaint);  
    }  
  
    public synchronized void setText(String text) {
        this.mText = text;  
        drawableStateChanged();  
    }  
  
    public void setTextColor(int color) {  
        mPaint.setColor(color);  
        drawableStateChanged();
    }
    
    public void setTextSize(float size) {
    	mPaint.setTextSize(size);
    	drawableStateChanged();
    }
}  
